//
//  ListViewController.m
//  10-20
//
//  Created by mac on 16/10/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ListViewController.h"
#import "ListLoginData.h"
#import "WaitCell.h"
@interface ListViewController ()
{
    NSMutableArray< ListLoginItemData *> *arr;
    NSInteger i,number;
    BOOL finish;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;


@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    arr = [NSMutableArray array];
    i = 1;
    finish = NO;
    self.tableview.editing = YES;
 
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return arr.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row < arr.count){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCell"];
        ListLoginItemData *d = arr[indexPath.row];
        cell.textLabel.text = d.tel;
        cell.detailTextLabel.text = d.name;
        return cell;
    }
    else
    {
        WaitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"waitCell"];
        if(finish){
            NSString *string = @"没有更多数据了";
            
            NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];
            
            [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 1)];
            cell.waitLabel.attributedText = attriString;
            
            return cell;
        }
        
        cell.waitLabel.text = @"";
        [cell.actv startAnimating];
        NSLog(@"i=%ld",i);
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/list.php?page=%ld",i++]];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
     
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            ListLoginData *d = [[ListLoginData alloc] initWithDictionary:dict];
            [arr addObjectsFromArray:d.record];
            NSLog(@"count=%ld",d.record.count);
            dispatch_async(dispatch_get_main_queue(), ^{
                if(d.record.count == 0)
                {
                    finish = YES;
                }
                [tableView reloadData];
            });
            
        }];
        [task resume];
        return cell;
    }
    
}


- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *a1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [arr removeObjectAtIndex:indexPath.row];
        NSLog(@"action");
        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }];
    
    return @[a1];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
